package com.moody.init;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.moody.hibernate.dao.CodeLibDAO;
import com.moody.hibernate.domain.CodeLib;

/*
 * Copyright © Moody analytics   
 *
 * @Title: ParamModelInit.java 
 * @Package： com.moody.init 
 * @author： Fuqiang Wang
 * @Description: 参数管理   
 * @date： 2012-3-30 下午5:22:12
 * @Modified by: Fuqiang Wang 
 * @version V1.0   
 */

public class ParamModelInit {
	private static final Logger log = LoggerFactory
			.getLogger(ParamModelInit.class);
	private static ParamModelInit instance = null;
	static WebApplicationContext web = ContextLoader.getCurrentWebApplicationContext();
	static ServletContext context = web.getServletContext();  
	static WebApplicationContext ctx  = WebApplicationContextUtils.getWebApplicationContext(context);
	private static CodeLibDAO codeLibDAO = CodeLibDAO.getFromApplicationContext(ctx);
	
	private ParamModelInit() {
	}
	
	public static ParamModelInit getInstance(){
		if(instance == null){
			instance = new ParamModelInit();
		}
		return instance;
	}
	
	//杠杆系数
	public List<CodeLib> getLeverParams(){
		Map<String,CodeLib> leverParamMap = DataDictionary.getInstance().getDataDictionary().get("LeverParam");
		int size = leverParamMap.size();
		CodeLib[] array = new CodeLib[size];
		List<CodeLib> list = new ArrayList<CodeLib>();
		Iterator<String> it = leverParamMap.keySet().iterator();
		while(it.hasNext()){
			CodeLib item = leverParamMap.get(it.next());
			array[Integer.parseInt(item.getItemDesc()) - 1] =  item;
		}
		for(int i=0; i < size; i++){
			list.add(array[i]);
		}
		return list;
	}
	
	//行业调整系数
	public List<CodeLib> getIndustryParams(){
		List<CodeLib> list = new ArrayList<CodeLib>();
		Iterator it = DataDictionary.getInstance().getDataDictionary().get("IndustryParam").keySet().iterator();
		while(it.hasNext()){
			String key = it.next().toString();
			list.add(DataDictionary.getInstance().getDataDictionary().get("IndustryParam").get(key));
		}
		
		return list;
	}	
	//金融质押折扣系数
	public List<CodeLib> getDiscountParams(){
		List<CodeLib> list = new ArrayList<CodeLib>();
		Iterator it = DataDictionary.getInstance().getDataDictionary().get("DiscountParam").keySet().iterator();
		while(it.hasNext()){
			String key = it.next().toString();
			list.add(DataDictionary.getInstance().getDataDictionary().get("DiscountParam").get(key));
		}
		
		return list;
	}
	
	//地区调整系数
	public List<CodeLib> getAreaParams(){
		List<CodeLib> list = new ArrayList<CodeLib>();
		Iterator it = DataDictionary.getInstance().getDataDictionary().get("AreaParam").keySet().iterator();
		while(it.hasNext()){
			String key = it.next().toString();
			list.add(DataDictionary.getInstance().getDataDictionary().get("AreaParam").get(key));
		}
		
		return list;
	}
	
	//偿债能力系数
	public List<CodeLib> getSolvencyParams(){
		List<CodeLib> list = new ArrayList<CodeLib>();
		Iterator it = DataDictionary.getInstance().getDataDictionary().get("SolvencyParam").keySet().iterator();
		while(it.hasNext()){
			String key = it.next().toString();
			list.add(DataDictionary.getInstance().getDataDictionary().get("SolvencyParam").get(key));
		}
		
		return list;
	}
	
	//CCF配置
	public List<CodeLib> getCCFParams(){
		List<CodeLib> list = new ArrayList<CodeLib>();
		Iterator it = DataDictionary.getInstance().getDataDictionary().get("CCFParam").keySet().iterator();
		while(it.hasNext()){
			String key = it.next().toString();
			list.add(DataDictionary.getInstance().getDataDictionary().get("CCFParam").get(key));
		}
		
		return list;
	}
	
	//零售客户准入分数线
	public List<CodeLib> getAccessLine(){
		List<CodeLib> list = new ArrayList<CodeLib>();
		Iterator it = DataDictionary.getInstance().getDataDictionary().get("IndAccessLine").keySet().iterator();
		while(it.hasNext()){
			String key = it.next().toString();
			list.add(DataDictionary.getInstance().getDataDictionary().get("IndAccessLine").get(key));
		}
		
		return list;
	}
	
	// 限额参数
	public List<CodeLib> getLimitParams(){
		List<CodeLib> list = new ArrayList<CodeLib>();
		Iterator it = DataDictionary.getInstance().getDataDictionary().get("CustLimitParam").keySet().iterator();
		while(it.hasNext()){
			String key = it.next().toString();
			list.add(DataDictionary.getInstance().getDataDictionary().get("CustLimitParam").get(key));
		}
		
		return list;
	}	
	// FTP参数
	public List<CodeLib> getFTPParams(){
		List<CodeLib> list = new ArrayList<CodeLib>();
		list = codeLibDAO.findByCodeNoOrderByItemDesc("FTPParam");
		return list;
	}
}
